package com.smartcat.web.actions.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.smartcat.model.GroupInfo;
import com.smartcat.model.UserGroupMap;
import com.smartcat.model.UserInfo;
import com.smartcat.service.GroupService;
import com.smartcat.service.UserService;
import com.smartcat.web.BaseAction;

@Controller
public class GroupAdminAction extends BaseAction {

	private GroupService groupService;
	private UserService userService;
	private List<GroupInfo> groupInfoList;
	private List<UserInfo> allUsers;
	
	private String groupname;
	private String groupdescription;
	private Integer groupInfoId;
	
	private List<Integer> selectedFunctions;
	
	private String jsonMsg;
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Autowired
	public void setGroupService(GroupService groupService){
		this.groupService = groupService;
	}
	
	
	public void setGroupInfoList(List<GroupInfo> groupInfoList){
		this.groupInfoList = groupInfoList;
	}
	
	
	public List<GroupInfo> getGroupInfoList() {
		return groupInfoList;
	}

	@Override
	public String execute() throws Exception {
		 groupInfoList = groupService.getAllGroupInfo();
		 allUsers =userService.getAllUsers();
		return SUCCESS;
	}
	
	public String createGroup(){
		GroupInfo groupInfo=new GroupInfo();
		groupInfo.setGroupName(this.groupname);
		groupInfo.setDescription(this.groupdescription);
		groupInfo.setStartDate(new Date());
		
		List<UserGroupMap> users=new ArrayList<UserGroupMap>();
		if(this.selectedFunctions!=null && this.selectedFunctions.size()>0){
			for(Integer selectId:this.selectedFunctions){
				UserGroupMap user=new UserGroupMap();
				user.setUserId(selectId);
				user.setStartDate(new Date());
				users.add(user);
			}
		
		}
		//this.groupService.createGroupInfo(groupInfo);
		this.groupService.createGroupInfoWithMembers(groupInfo, users);
		return SUCCESS;
	}

	public String deleteGroup(){
		this.groupService.deleteGroupById(this.groupInfoId);
		return SUCCESS;
	}

	public String getGroupname() {
		return groupname;
	}


	public void setGroupname(String groupname) {
		this.groupname = groupname;
	}


	public String getGroupdescription() {
		return groupdescription;
	}


	public void setGroupdescription(String groupdescription) {
		this.groupdescription = groupdescription;
	}


	public Integer getGroupInfoId() {
		return groupInfoId;
	}


	public void setGroupInfoId(Integer groupInfoId) {
		this.groupInfoId = groupInfoId;
	}


	public String getJsonMsg() {
		return jsonMsg;
	}


	public void setJsonMsg(String jsonMsg) {
		this.jsonMsg = jsonMsg;
	}


	public List<UserInfo> getAllUsers() {
		return allUsers;
	}


	public void setAllUsers(List<UserInfo> allUsers) {
		this.allUsers = allUsers;
	}
	public List<Integer> getSelectedFunctions() {
		return selectedFunctions;
	}
	public void setSelectedFunctions(List<Integer> selectedFunctions) {
		this.selectedFunctions = selectedFunctions;
	}

	
	
}
